﻿//using Kingdee.BOS.App.Data;
//using Kingdee.BOS.Core.DynamicForm;
//using Kingdee.BOS.Core.DynamicForm.PlugIn;
//using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
//using Kingdee.BOS.Orm.DataEntity;
//using Kingdee.BOS.Util;
//using Kingdee.K3.FIN.GL.Business.PlugIn.BizVoucher;
//using Newtonsoft.Json.Linq;
//using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace CB.KM.ZH.SalOutstockGenerateZeroCostBatchMaintain3
//{
//    [Description("【动态表单】成本单据条件选择动态表单"), HotUpdate]
//   public class ImportCostBillForm : AbstractDynamicFormPlugIn
//    {
//        public string LogType { get; set; } = "生成零成本批量维护";

//        public override void ButtonClick(ButtonClickEventArgs e)
//        {
//            base.ButtonClick(e);
//            //确定
//            if (e.Key.EqualsV1("FbtnSubmit"))
//            {
//                try
//                {
//                    Main();
//                }
//                catch (Exception ex)
//                {
//                    LogHelper.CBLog($"异常Cost0001\n【msg:{ex.Message}】", LogType);
//                    View.ShowErrMessage(ex.Message);
//                }
//            }
//            else if(e.Key.EqualsV1("FbtnClose"))
//            {
//                View.Close();
//            }
//        }
//        public void Main()
//        {
//            int orgId = 0;
//            string orgNumber = string.Empty;
//            string hstxNumber = string.Empty;
//            string kjzcNumber = string.Empty;
//            int stockId = 0;
//            string stockNumber = string.Empty;
//            string datestr = Model.GetValue("FDate").ToStringV1();
//            DynamicObject orgObj = Model.GetValue("FOrg") as DynamicObject;
//            if (orgObj!=null)
//            {
//                orgId = orgObj["Id"].ToStringV1().ToInt32V1() ;
//                orgNumber = orgObj["Number"].ToStringV1();
//            }
//            DynamicObject hstxObj = Model.GetValue("FHSTX") as DynamicObject;
//            if (hstxObj != null)
//                hstxNumber = hstxObj["Number"].ToStringV1();
//            DynamicObject kjzcObj = Model.GetValue("FKJZC") as DynamicObject;
//            if (kjzcObj != null)
//                kjzcNumber = kjzcObj["Number"].ToStringV1();
//            DynamicObject stockObj = Model.GetValue("FStock") as DynamicObject;
//            if (stockObj != null)
//            {
//                stockId = stockObj["Id"].ToStringV1().ToInt32V1();
//                stockNumber = stockObj["Number"].ToStringV1();
//            }
//            if (orgNumber.IsNullOrEmptyOrWhiteSpace()|| hstxNumber.IsNullOrEmptyOrWhiteSpace() || kjzcNumber.IsNullOrEmptyOrWhiteSpace() || stockNumber.IsNullOrEmptyOrWhiteSpace() || datestr.IsNullOrEmptyOrWhiteSpace())
//            {
//                View.ShowErrMessage($"组织，核算体系，会计政策，仓库，日期不可为空!!!");
//                return;
//            }
//            string date1 = datestr.ToDateTime().ToString("yyyy-MM-01");
//            string date2 = date1.ToDateTime().AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59");

//            string sql = $@"/*dialect*/ select 
//id
//,billNo
//,entyId
//,orgId
//,stockId
//,stockNumber
//,stockName
//,matNumber
//,averageCost=allCost/allQty
//from (
//select 
//id=main.FID
//,billNo=main.FBILLNO
//,entyId=enty.FENTRYID
//,orgId=main.FSTOCKORGID
//,stockId=enty.FSTOCKID
//,stockNumber=stock.FNUMBER
//,stockName=stockL.FNAME
//,matNumber=mat.FNUMBER
//,cost=entyF.FCOSTAMOUNT
//,qty=enty.FREALQTY
//,num=ROW_NUMBER() over(partition by main.FSTOCKORGID,enty.FSTOCKID,mat.FNUMBER order by enty.FENTRYID)
//,allCost=SUM(entyF.FCOSTAMOUNT) over(partition by main.FSTOCKORGID,enty.FSTOCKID,mat.FNUMBER)
//,allQty=SUM(enty.FREALQTY) over(partition by main.FSTOCKORGID,enty.FSTOCKID,mat.FNUMBER)
//from T_SAL_OUTSTOCKEntry enty 
//left join T_SAL_OUTSTOCKENTRY_F entyF on enty.FENTRYID=entyF.FENTRYID
//left join T_SAL_OUTSTOCK main on enty.FID=main.FID
//left join T_BD_STOCK stock on enty.FSTOCKID=stock.FSTOCKID
//left join T_BD_STOCK_L stockL on stock.FSTOCKID=stockL.FSTOCKID and stockL.FLOCALEID=2052
//left join T_BD_MATERIAL mat on enty.FMATERIALID=mat.FMATERIALID
//where main.FDOCUMENTSTATUS='C'
//and main.FSTOCKORGID={orgId}
//and enty.FSTOCKID={stockId}
//and main.FDATE>='{date1}'
//and main.FDATE<='{date2}'
//and ISNULL(entyF.FCOSTAMOUNT,0)>0
//)A where num=1";

//            DynamicObjectCollection datas = DBUtils.ExecuteDynamicObject(Context,sql);
//            List<SalOutstockData> salList = datas.DynamicObjectCollectionToDataList<SalOutstockData>();
//            if (salList == null || salList.Count < 1)
//            {
//                View.ShowErrMessage("无有效数据!!!");
//                return;
//            }
//            SalOutstockData firstData = salList.FirstOrDefault();
//            string json = string.Empty;
//            {
//                JObject Root = new JObject();
//                Root["IsAutoSubmitAndAudit"] = true;
//                JObject Model = new JObject();
//                //核算体系
//                JObject FACCTGSYSTEMID = new JObject();
//                FACCTGSYSTEMID["FNumber"] =hstxNumber;
//                Model["FACCTGSYSTEMID"] = FACCTGSYSTEMID;
//                //核算组织
//                JObject FACCTGORGID = new JObject();
//                FACCTGORGID["FNumber"] =orgNumber;
//                Model["FACCTGORGID"] = FACCTGORGID;
//                //会计政策
//                JObject FACCTPOLICYID = new JObject();
//                FACCTPOLICYID["FNumber"] = kjzcNumber;
//                Model["FACCTPOLICYID"] = FACCTPOLICYID;
//                //名称
//                Model["FName"] = $"{firstData.stockNumber}_{firstData.stockName}";
//                JArray entyArray = new JArray();
//                foreach (SalOutstockData sal in salList)
//                {
//                    JObject entyObj = new JObject();
//                    //物料
//                    JObject FMATERIALID = new JObject();
//                    FMATERIALID["FNumber"] = sal.matNumber;
//                    entyObj["FMATERIALID"] = FMATERIALID;
//                    //单价(平均成本)
//                    entyObj["FPrice"] = sal.averageCost;

//                    entyArray.Add(entyObj);
//                }
//                Model["FEntity"] = entyArray;
//                Root["Model"] = Model;
//                json = Root.ToStringV1();
//            }
//            string loginMsg = string.Empty;
//            if (!K3Helper.Login(Context,ref loginMsg))
//            {
//                if (!K3Helper.Login(Context, ref loginMsg))
//                {
//                    LogHelper.CBLog($"登录失败!!!\n【{loginMsg}】", LogType);
//                    View.ShowErrMessage($"登录失败!!!\n【{loginMsg}】");
//                    return;
//                }
//            }
//            string msg = string.Empty;
//            bool isOk = K3Helper.Save(Context, "HS_BATCHESTIMATE", json,ref msg);
//            if (isOk)
//                LogHelper.CBLog($"保存成功\n【json:{json}】", LogType);
//            else
//            {
//                LogHelper.CBLog($"保存失败\n【msg:{msg}】\n【json:{json}】", LogType);
//                View.ShowErrMessage($"保存失败\n【msg:{msg}】\n【json:{json}】");
//            }

//        }

//    }
//    public class SalOutstockData
//    {
//        public int id { get; set; }
//        public string billNo{ get; set; }
//        public int entyId { get; set; }
//        public int orgId { get; set; }
//        public int stockId { get; set; }
//        public string stockNumber { get; set; }
//        public string stockName { get; set; }
//        public string matNumber { get; set; }
//        public decimal averageCost { get; set; }
//    }
//}
